﻿using System.Data;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
using System.Collections;

namespace CPDataGridUserControl
{
    public partial class DataGridViewWithPaging : UserControl
    {
        private int _CurrentPage = 1;

        private string _FirstButtonText = string.Empty;
        public string FirstButtonText
        {
            get
            {
                if (_FirstButtonText == string.Empty)
                    return btnFirst.Text;
                else
                    return _FirstButtonText;
            }
            set
            {
                _FirstButtonText = value;
                btnFirst.Text = _FirstButtonText;
            }
        }

        private string _LastButtonText = string.Empty;
        public string LastButtonText
        {
            get
            {
                if (_LastButtonText == string.Empty)
                    return btnLast.Text;
                else
                    return _LastButtonText;
            }
            set
            {
                _LastButtonText = value;
                btnLast.Text = _LastButtonText;
            }
        }

        private string _PreviousButtonText = string.Empty;
        public string PreviousButtonText
        {
            get
            {
                if (_PreviousButtonText == string.Empty)
                    return btnPrevious.Text;
                else
                    return _PreviousButtonText;
            }
            set
            {
                _PreviousButtonText = value;
                btnPrevious.Text = _PreviousButtonText;
            }
        }

        private string _NextButtonText = string.Empty;
        public string NextButtonText
        {
            get
            {
                if (_NextButtonText == string.Empty)
                    return btnNext.Text;
                else
                    return _NextButtonText;
            }
            set
            {
                _NextButtonText = value;
                btnNext.Text = _NextButtonText;
            }
        }


        private int _Width;
        public int ControlWidth
        {
            get
            {
                if (_Width == 0)
                    return dataGridView1.Width;
                else
                    return _Width;
            }
            set
            {
                _Width = value;
                dataGridView1.Width = _Width;
            }
        }

        private int _Height;
        public int ControlHeight
        {
            get
            {
                if (_Height == 0)
                    return dataGridView1.Height;
                else
                    return _Height;
            }
            set
            {
                _Height = value;
                dataGridView1.Height = _Height;
            }
        }

        private int _PateSize = 10;
        public int PageSize
        {
            get
            {
                return _PateSize;
            }
            set
            {
                _PateSize = value;
            }
        }


        private DataTable _DataSource;
        public DataTable DataSource
        {
            get
            {
                return _DataSource;
            }
            set
            {
                _DataSource = value;
            }
        }

        private XDocument _XmlDataSource;
        public XDocument XmlDataSource
        {
            get
            {
                return _XmlDataSource;
            }
            set
            {
                _XmlDataSource = value;
                if (_XmlDataSource != null)
                {
                    XmlReader reader = _XmlDataSource.CreateReader();
                    DataSource.ReadXml(reader);
                }
            }
        }

        public DataGridViewWithPaging()
        {
            InitializeComponent();
        }

        private void btnFirst_Click(object sender, System.EventArgs e)
        {
            _CurrentPage = 1;
            dataGridView1.DataSource = ShowData(_CurrentPage);
        }

        private void btnPrevious_Click(object sender, System.EventArgs e)
        {
            _CurrentPage -= 1;
            dataGridView1.DataSource = ShowData(_CurrentPage);
        }

        private void btnNext_Click(object sender, System.EventArgs e)
        {
            _CurrentPage += 1;
            dataGridView1.DataSource = ShowData(_CurrentPage);
        }

        private void btnLast_Click(object sender, System.EventArgs e)
        {
            _CurrentPage = DataSource.Rows.Count / PageSize;
            dataGridView1.DataSource = ShowData(DataSource.Rows.Count / PageSize);
        }

        public void DataBind(DataTable dataTable)
        {
            DataSource = dataTable;
            dataGridView1.DataSource = ShowData(1);
        }

        public void DataBind(XDocument xDocument)
        {
            XmlDataSource = xDocument;
            dataGridView1.DataSource = ShowData(1);
        }

        private IList ShowData(int pageNumber)
        {
            int startIndex = PageSize * (pageNumber - 1);
            txtPaging.Text = string.Format("Page {0} Of {1} Pages", pageNumber, DataSource.Rows.Count / PageSize);
            return DataSource.AsEnumerable().Where((s, k) => k >= startIndex && k < (startIndex + PageSize)).ToList();
        }
    }
}
